//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.LiveTaskUserChannel;
import com.ruoyi.live.base.mapper.LiveTaskUserChannelMapper;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;

@Service
@Scope("prototype")
public class UploadService {
    private static final Logger log = LoggerFactory.getLogger(UploadService.class);
    @Autowired
    LiveTaskUserChannelMapper channelMapper;
    @Autowired
    VideoService videoService;

    public UploadService() {
    }

    public Message uploadRecordFile(Long channelId, String channelName, MultipartFile file, HttpServletRequest request) {
        Integer uploadState = 2;

        try {
            if (!ICommons.isNullOrEmpty(channelId) && !ICommons.isNullOrEmpty(channelName) && file != null) {
                LiveTaskUserChannel channel = (LiveTaskUserChannel)this.channelMapper.selectById(channelId);
                if (channel == null) {
                    throw new Exception("channel不能为null");
                } else if (!channel.getChannelName().equals(channelName)) {
                    throw new Exception("channelName不正确");
                } else if (!SessionInfo.getUserId().equals(channel.getCreateUser())) {
                    throw new Exception("非法上传，上传用户和直播用户非一个");
                } else {
                    String fileName = file.getOriginalFilename();
                    if (ICommons.isNullOrEmpty(fileName)) {
                        throw new Exception("文件名不能为空");
                    } else {
                        if (fileName.indexOf("\\") != -1) {
                            fileName = fileName.substring(fileName.lastIndexOf("\\"));
                        }

                        String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
                        if (ICommons.isNullOrEmpty(ext)) {
                            throw new Exception("扩展名不能为空");
                        } else if (ext.contains("jsp")) {
                            throw new Exception("你要干嘛?");
                        } else {
                            String filePath = request.getServletContext().getRealPath("/static/static/data/record/");
                            File targetFile = new File(filePath);
                            if (!targetFile.exists()) {
                                targetFile.mkdirs();
                            }

                            String name = channel.getChannelName() + ext;

                            try {
                                File dest = new File(filePath + name);
                                file.transferTo(dest);

                                try {
                                    uploadState = this.videoService.getScreenVideoInfo(channelId, request.getSession().getServletContext().getRealPath(""), channel.getChannelName());
                                } catch (Exception var14) {
                                    var14.printStackTrace();
                                }

                                this.channelMapper.updateById((new LiveTaskUserChannel()).setId(channelId).setUploadState(uploadState));
                            } catch (Exception var15) {
                                var15.printStackTrace();
                                throw new Exception("上传失败");
                            }

                            return new Message(true, "");
                        }
                    }
                }
            } else {
                throw new Exception("参数错误");
            }
        } catch (Exception var16) {
            log.error("上传视频文件失败", var16);
            return new Message(false, "");
        }
    }
}
